package com.block.api.controller.expose;

import com.alibaba.fastjson.JSONObject;
import com.block.channelunion.service.IChannleUnionLoginService;
import com.block.common.exception.ApiException;
import com.block.common.model.vo.ResultInfoVO;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("apiunion/channel")
public class UnionController {

    @Autowired
    IChannleUnionLoginService channleUnionLoginService;

    @RequestMapping(value = "/unionCheck", method = {RequestMethod.POST})
    public String unionCheck(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                             @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String mobileMd5 = tranData.getString("mobileMd5");
        String userIp = tranData.getString("userIp");
        ResultInfoVO resultInfoVO = channleUnionLoginService.unionCheck(mobileMd5, userIp, applicationInfo, channelBaseInfo);
        return resultInfoVO.toResp();
    }

    @RequestMapping(value = "/unionLogin", method = {RequestMethod.POST})
    public String unionLogin(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "applicationInfo") ApplicationInfo applicationInfo,
                             @RequestAttribute(value = "channelBaseInfo") ChannelBaseInfo channelBaseInfo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String mobile = tranData.getString("mobile");
        String orderNo = tranData.getString("orderNo");
        String userIp = tranData.getString("userIp");
        ResultInfoVO resultInfoVO = channleUnionLoginService.unionLogin(mobile, orderNo, userIp, applicationInfo, channelBaseInfo);
        return resultInfoVO.toResp();
    }

}
